package com.traffic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.traffic.annotation.Power;
import com.traffic.constant.Status;
import com.traffic.entity.User;
import com.traffic.entity.UserInfo;
import com.traffic.exception.BaseException;
import com.traffic.mapper.UserInfoMapper;
import com.traffic.mapper.UserMapper;
import com.traffic.service.ResourceService;
import com.traffic.service.UserInfoService;
import com.traffic.util.JwtUtil;
import com.traffic.constant.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/9/4
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ResourceService resourceService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 保存用户信息
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo saveInfo(UserInfo userInfo) {
        if (check(userInfo.getPhone())) {
            userInfoMapper.insert(userInfo);
            return userInfo;
        }
        throw new BaseException(Status.DATABASE_ERROR);
    }

    /**
     * 用户更新账户信息
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo updateInfo(UserInfo userInfo) {
//        if (check(userInfo.getPhone())) {
//            userInfoMapper.updateById(userInfo);
//            return userInfo;
//        }
//        throw new BaseException(Status.DATABASE_ERROR);
        userInfoMapper.updateById(userInfo);
        return userInfo;
    }

    /**
     * 通过手机号查询用户信息
     * @param phone
     * @return
     */
    @Override
    public UserInfo queryInfo(String phone) {
        if (check(phone)) {
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", phone);
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            if(ObjectUtil.isNotNull(userInfo.getHeadImg())) {
                String path = resourceService.get(userInfo.getHeadImg()).getUrl();
                userInfo.setHeadImgPath(path);
            }
            return userInfo;
        }
        throw new BaseException(Status.DATABASE_ERROR);
    }

    /**
     * 用户头像更新
     * @param account 用户账户
     * @param id 图片id
     * @return
     */
    @Override
    public UserInfo addOrUpdateHeadImg(String account, Long id) {
        if (check(account)) {
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", account);
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            userInfo.setHeadImg(id);
            updateInfo(userInfo);
            return userInfo;
        }
        throw new BaseException(Status.TOKEN_ERROR);
    }

    /**
     * 管理员修改用户信息 userinfo中是有id的
     * @param userInfo
     * @return
     */
    @Override
    @Power(authority = Const.AUTH_EDIT)
    public UserInfo editUserInfo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
        return userInfo;
    }

    /**
     * 通过userid查询用户信息
     * @param userId
     * @return
     */
    @Override
   // @Power(authority = Const.AUTH_QUERY)
    public UserInfo getUserInfoByUserId(Long userId) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return userInfoMapper.selectOne(wrapper);
    }

    /**
     * 通过phone查询用户信息
     * @param phone
     * @return
     */
    @Override
    @Power(authority = Const.AUTH_QUERY)
    public UserInfo getUserInfoByPhone(String phone) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return userInfoMapper.selectOne(wrapper);
    }

    /**
     * 管理员修改用户信息 包括手机号字段的情况
     * 同步更新user表中的手机号
     * @param userInfo
     * @return
     */
    @Override
    @Power(authority = Const.AUTH_EDIT)
    @Transactional
    public UserInfo editUserInfoIncludePhone(UserInfo userInfo) {
        Long userId = userInfo.getUserId();
        User user = userMapper.selectById(userId);
        user.setAccount(userInfo.getPhone());
        userInfoMapper.updateById(userInfo);
        userMapper.updateById(user);
        return userInfo;
    }

    /**
     * 管理员删除用户账户 传入的是user表的主键
     * @param userId
     * @return
     */
    @Override
    @Transactional
    @Power(authority = Const.AUTH_DELETE)
    public Boolean deleteUser(Long userId) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        User user = userMapper.selectById(userId);
        Integer resultUserInfo = userInfoMapper.deleteById(userInfo.getId());
        Integer resultUser = userMapper.deleteById(user.getId());
        if (resultUserInfo > 0 && resultUser > 0) {
            return true;
        }else {
            return false;
        }
    }

    private boolean check(String account) {
        return true;
//        if (jwtUtil.check(account)) {
//            return true;
//        }else {
//            throw new BaseException(Status.TOKEN_ERROR);
//        }
    }

}
